//
//  YRVisitorView.swift
//  新浪微博
//
//  Created by Viper on 16/6/25.
//  Copyright © 2016年 Viper. All rights reserved.
//

import UIKit
import SnapKit
class YRVisitorView: UIView {

    var closure : (()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //MARK: - 设置视图
    
    private func setupUI(){
        
        backgroundColor = RGB(237, green: 237, blue: 237)
        //添加控件
        addSubview(feedImageView)
        
        addSubview(coverImageView)
        
        addSubview(iconImageView)
        
        addSubview(messageLabel)
        
        addSubview(loginButton)
        
        addSubview(registerButton)
        
        //控件约束
        
        feedImageView.snp_remakeConstraints { (make) in
            
            make.center.equalTo(self)
        
        }
   
        coverImageView.snp_remakeConstraints { (make) in
            
            make.center.equalTo(self)
        }
     
        iconImageView.snp_makeConstraints { (make) in
            
            make.center.equalTo(self)
        }
      
        messageLabel.snp_makeConstraints { (make) in
            
           make.top.equalTo(coverImageView.snp_bottom).offset(16)
           make.width.equalTo(224)
           make.height.equalTo(36)
           make.centerX.equalTo(self)
            
        }
        
        loginButton.snp_makeConstraints { (make) in
            
            make.leading.equalTo(messageLabel).offset(10)
           make.size.equalTo(CGSize(width: 100, height: 36))
           make.top.equalTo(messageLabel.snp_bottom).offset(16)
        }
        
     
        registerButton.snp_makeConstraints { (make) in
            
            make.trailing.equalTo(messageLabel).offset(10)
           make.size.equalTo(CGSize(width: 100, height: 36))
           make.top.equalTo(messageLabel.snp_bottom).offset(16)
        }
        
    }
    //MARK: - 设置动画
    
    func setupAnim (){
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //设置动画时间
        anim.duration = 10
        //设置旋转角度
        anim.toValue = M_PI * 2
        //设置动画次数
        anim.repeatCount = MAXFLOAT
        //当我们APP推到后台或者切换控制器的时候 动画默认完成
        anim.removedOnCompletion = false
        //添加动画
        feedImageView.layer.addAnimation(anim, forKey: nil)
    }
    
    
    //MARK: - 供外界调用的方法

    func setupVisitonViewInfo(imageName : String?,title :String?){
       
        //如果有值就代表不是主页
        if let img = imageName,let tit = title{
            
          iconImageView.image = UIImage(named: img)
           messageLabel.text = tit
            feedImageView.hidden = true
            
        }else{
       
            setupAnim ()
        }
    }
  
    
  //MARK: - 懒加载控件
    //圆图
    private lazy var feedImageView : UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    //遮盖图
    private lazy var coverImageView : UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    //房子图
    private lazy var iconImageView : UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")
    //信息栏
    private lazy var messageLabel : UILabel = {
       
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.textAlignment = .Center
        label.font = UIFont.systemFontOfSize(14)
        label.textColor = ThemeColor
        label.numberOfLines = 0
        
        return label
    }()
    //登录按钮
    private lazy var loginButton : UIButton = {
        
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        btn.addTarget(self, action: #selector(YRVisitorView.buttonClick), forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    //注册按钮
    private lazy var registerButton : UIButton = {
    
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        btn.addTarget(self, action: #selector(YRVisitorView.buttonClick), forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    
  @objc private func buttonClick(){
    //执行闭包
       closure?()
    }

    
}
